package com.molichuxing.gateway.api.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = { "com.molichuxing.gateway.api.services.impl" })
public class SwagerConfig {

	@Value("${swagger.enable}")
	private boolean enable;

	@Bean
	public Docket customDocket() {
		// token
		ParameterBuilder tokenBuilder = new ParameterBuilder();
		tokenBuilder.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header")
				.required(false).build();
		List<Parameter> parameters = new ArrayList<Parameter>();
		parameters.add(tokenBuilder.build());

		return new Docket(DocumentationType.SWAGGER_2).enable(enable).apiInfo(apiInfo())
				.globalOperationParameters(parameters).select()
				// 扫描该包下的所有需要在Swagger中展示的API,
				// 如果某个接口不想暴露,可以使用@ApiIgnore这样,该接口就不会暴露
				.apis(RequestHandlerSelectors.basePackage("com.molichuxing.gateway.api.services.impl"))
				.paths(PathSelectors.any())
				.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				// 页面标题
				.title("经销商网关API接口")
				// 描述
				.description("经销商网关API接口")
				// 创建人信息
				.contact(new Contact("经销商网关", "", ""))
				// 版本号
				.version("1.0.0").build();
	}
}
